package kliment.api.google
{
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;

	import kliment.api.ICheckout;
	import kliment.data.ShopingCartItem;

	/**
	 *
	 */
	public class PayPalCheckout implements ICheckout
	{
		static private const _API_URL:String = "https://www.paypal.com/cgi-bin/webscr";
		static private const _cmd:String = '_cart';
		static private const _upload:uint = 1;
		static private const _address_override:uint = 1;
		static private const _rm:uint = 2;
		static private const _no_shipping:uint = 1;
		static private const _charset:String = "utf-8";

		/**
		 * Your PayPal ID or an email address associated with your PayPal account. Email addresses must be confirmed.
		 */
		public var business:String = 'your@youremail.com';

		/**
		 * The currency of prices for trial periods and the subscription. The default is USD.
		 * For allowable values, see <a href="https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_api_nvp_currency_codes">Currency Codes</a>.
		 */
		public var currency_code:String = 'usd';

		/**
		 * The URL to which PayPal posts information about the transaction, in the form of Instant Payment Notification messages.
		 */
		public var notify_url:String;

		/**
		 *
		 */
		public var return_url:String;

		/**
		 * First name
		 */
		public var first_name:String;

		/**
		 * Last name
		 */
		public var last_name:String;

		/**
		 * Sets shipping and billing country.
		 * For allowable values, see <a href="https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_api_nvp_country_codes">Countries and Regions Supported by PayPal</a>.
		 */
		public var country_code:String;

		/**
		 * State;
		 * use <a href="http://www.usps.com/ncsc/lookups/abbreviations.html#states">Official U.S. Postal Service Abbreviations</a>.
		 */
		public var state_code:String;

		/**
		 * City
		 */
		public var city:String;

		/**
		 * Postal code
		 */
		public var zip:String;

		/**
		 * Street (1 of 2 fields)
		 */
		public var address1:String;

		/**
		 * Street (2 of 2 fields)
		 */
		public var address2:String;

		/**
		 * The three-digit prefix for U.S. phone numbers,
		 * or the entire phone number for phone numbers outside the U.S.,
		 * excluding country code. This will prepopulate the payer’s home phone number.
		 */
		public var phone:String;

		/**
		 * Email address
		 */
		public var email:String;

		private var _itemsCollection:Array = [];

		/**
		 * Constructor.
		 */
		public function PayPalCheckout()
		{

		}

		/**
		 * @inheritDoc
		 */
		public function get apiURL():String
		{
			return _API_URL;
		}

		/**
		 * @inheritDoc
		 */
		public function getRequestVariables():URLVariables
		{
			var urlVars:URLVariables = new URLVariables();
			urlVars.business = business;
			urlVars.currency_code = currency_code;
			urlVars.cmd = _cmd;
			urlVars.no_shipping = _no_shipping;
			urlVars.upload = _upload;
			urlVars.address_override = _address_override;
			urlVars.rm = _rm;
			urlVars.charset = _charset;

			if (_validVar(notify_url))
				urlVars.notify_url = notify_url;

			if (_validVar(return_url))
				urlVars.return_url = return_url;

			if (_validVar(first_name))
				urlVars.first_name = first_name;

			if (_validVar(last_name))
				urlVars.last_name = last_name;

			if (_validVar(country_code))
				urlVars.country_code = country_code;

			if (_validVar(state_code))
				urlVars.state = state_code;

			if (_validVar(zip))
				urlVars.zip = zip;

			if (_validVar(city))
				urlVars.city = city;

			if (_validVar(address1))
				urlVars.address1 = address1;

			if (_validVar(address2))
				urlVars.address2 = address2;

			if (_validVar(phone))
				urlVars.night_phone_b = phone;

			if (_validVar(email))
				urlVars.email = email;
			return urlVars;
		}

		private function _validVar(value:String = null):Boolean
		{
			return  (value && value != '');
		}

		/**
		 * @inheritDoc
		 */
		public function submit():Boolean
		{
			var length:uint = _itemsCollection.length;
			if (!length)
			{
				return false;
			}

			var urlVars:URLVariables = getRequestVariables();

			var item:Object;
			for (var i:uint = 0; i < length; i++)
			{
				item = _itemsCollection[i];
				for (var j in item)
				{
					urlVars[j] = item[j];
				}
			}

			var urlRequest:URLRequest = new URLRequest(apiURL);
			urlRequest.method = URLRequestMethod.POST;
			urlRequest.data = urlVars;

			navigateToURL(urlRequest, "_blank");

			return true;
		}

		/**
		 * @inheritDoc
		 */
		public function attachProducts(list:Array):Boolean
		{
			var length:uint = list.length;
			_itemsCollection = [];

			if (length > 0)
			{
				//if (length > 1)
				{
					for (var i:uint = 0; i < length; i++)
					{
						_itemsCollection.push(convertCardItem(list[i], i + 1));
					}
				}
				/*else
				{
					_itemsCollection.push(convertCardItem(list[0]));
				}*/
				return true;
			}
			return false;
		}

		private function convertCardItem(value:ShopingCartItem, index:uint = 0):Object {
			var result:Object = new Object();
			//var suffix:String = (index > 0)?('_' + index):'';
			var suffix:String = '_' + index;
			result['item_number' + suffix] = value.id;
			result['amount' + suffix] = value.price;
			result['item_name' + suffix] = value.name;
			result['quantity' + suffix] = value.quantity;
			return result;
		}
	}

}